package cn.ibizlab.odoo.core.odoo_account.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Iaccount_account_type;
import cn.ibizlab.odoo.core.odoo_account.domain.Account_account_type;
import cn.ibizlab.odoo.core.odoo_account.filter.Account_account_typeSearchContext;
import cn.ibizlab.odoo.core.odoo_account.service.IAccount_account_typeService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[科目类型] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Account_account_typeServiceImpl implements IAccount_account_typeService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Iaccount_account_typeClientService account_account_typeClientService;

    @Override
    public void updateBatch(List<Account_account_type> account_account_types, int batchSize) {

        //account_account_typeClientService.updateBatch(account_account_types,batchSize) ;

    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //account_account_typeClientService.removeBatch(account_account_types) ;

    }

    @Override
    public Account_account_type get(Integer id) {
        Iaccount_account_type clientModel = account_account_typeClientService.createModel();
        clientModel.setId(id);
        account_account_typeClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Account_account_type get(Account_account_type account_account_type) {
        Account_account_type retaccount_account_type = this.get(account_account_type.getId()) ;
    	BeanCopier copier=BeanCopier.create(retaccount_account_type.getClass(), account_account_type.getClass(), false);
        copier.copy(retaccount_account_type, account_account_type, null);
        return account_account_type ;
    }

    @Override
    public void createBatch(List<Account_account_type> account_account_types, int batchSize) {

                        //account_account_typeClientService.createBatch(account_account_types,batchSize) ;

    }

    @Override
    public boolean update(Account_account_type account_account_type) {
        boolean bOk = false ;
        Iaccount_account_type clientModel = this.convert2Model(account_account_type,null);
        account_account_typeClientService.update(clientModel) ;
        this.convert2Domain(clientModel,account_account_type) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public boolean create(Account_account_type account_account_type) {
        boolean bOk = false ;
        Iaccount_account_type clientModel = this.convert2Model(account_account_type,null);
        account_account_typeClientService.create(clientModel) ;
        this.convert2Domain(clientModel,account_account_type);
        bOk = true ;

        return bOk ;
    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Iaccount_account_type clientModel = account_account_typeClientService.createModel();
        clientModel.setId(id);
        account_account_typeClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Account_account_type> searchDefault(Account_account_typeSearchContext context) {
        List<Account_account_type> list = new ArrayList<Account_account_type>();
        Page<cn.ibizlab.odoo.core.client.model.Iaccount_account_type> clientModelList = account_account_typeClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iaccount_account_type clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Account_account_type>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据用户查询
     */
    @Override
    public Page<Account_account_type> selectByOdoo_create(Integer id){
        Account_account_typeSearchContext context = new Account_account_typeSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Account_account_type> list = new ArrayList<Account_account_type>();
        Page<cn.ibizlab.odoo.core.client.model.Iaccount_account_type> clientModelList = account_account_typeClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iaccount_account_type clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Account_account_type>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Account_account_type> selectByOdoo_write(Integer id){
        Account_account_typeSearchContext context = new Account_account_typeSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Account_account_type> list = new ArrayList<Account_account_type>();
        Page<cn.ibizlab.odoo.core.client.model.Iaccount_account_type> clientModelList = account_account_typeClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iaccount_account_type clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Account_account_type>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Iaccount_account_type convert2Model(Account_account_type domain , Iaccount_account_type model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = account_account_typeClientService.createModel() ;
        }

         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getTypeDirtyFlag())
            model.setType(domain.getType());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getNoteDirtyFlag())
            model.setNote(domain.getNote());
         if(domain.getInclude_initial_balanceDirtyFlag())
            model.setInclude_initial_balance(domain.getInclude_initial_balance());
         if(domain.getNameDirtyFlag())
            model.setName(domain.getName());
         if(domain.getInternal_groupDirtyFlag())
            model.setInternal_group(domain.getInternal_group());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
        return model ;
    }

    public Account_account_type convert2Domain( Iaccount_account_type model ,Account_account_type domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Account_account_type();
        }

        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getTypeDirtyFlag())
            domain.setType(model.getType());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getNoteDirtyFlag())
            domain.setNote(model.getNote());
        if(model.getInclude_initial_balanceDirtyFlag())
            domain.setInclude_initial_balance(model.getInclude_initial_balance());
        if(model.getNameDirtyFlag())
            domain.setName(model.getName());
        if(model.getInternal_groupDirtyFlag())
            domain.setInternal_group(model.getInternal_group());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        return domain ;
    }


}
